package com.anlogic.sdk.ui.tests.uc4.filemgr;

import static org.junit.Assert.*;

import java.io.File;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swtbot.eclipse.finder.SWTWorkbenchBot;
import org.eclipse.swtbot.eclipse.finder.waits.Conditions;
import org.eclipse.swtbot.swt.finder.keyboard.Keystrokes;
import org.eclipse.swtbot.swt.finder.utils.SWTBotPreferences;
import org.eclipse.swtbot.swt.finder.waits.DefaultCondition;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell;
import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.anlogic.sdk.env.utils.EnvCheckUtils;
import com.anlogic.sdk.ui.tests.utils.WorkspaceUtils;
import com.anlogic.sdk.utils.EnvUtils;
import com.anlogic.sdk.utils.UIUtils;
import com.anlogic.sdk.workspace.FDWorkspace;

public class UC4_TC004_Test {
	
	private static SWTWorkbenchBot bot;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception {

		bot = new SWTWorkbenchBot();
		SWTBotPreferences.TIMEOUT = 10000;
		SWTBotPreferences.KEYBOARD_LAYOUT = "EN_US";
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"), 90 * 1000);
	//	SWTBotPreferences.PLAYBACK_DELAY = 10;
		
	}

	@Before
	public void setUp() throws Exception {
		WorkspaceUtils.resetworkbench(bot);
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
		
	}


	@After
	public void tearDown() throws Exception {
		
		WorkspaceUtils.cleanWorkspace();
	}

	@Test
	public void openProjectTest() throws Exception{
		bot.waitUntil(Conditions.shellIsActive("data - FutureDynasty"), 60 * 1000);

		bot.waitUntil(new DefaultCondition() {
			@Override
			public boolean test() throws Exception {
				// 检查菜单是否可用
				return bot.menu("File").menu("New").menu("Project...").isEnabled();
			}

			@Override
			public String getFailureMessage() {
				return "Menu 'Platform Project' is not available or not enabled.";
			}
		}, 60 * 1000);
		bot.menu("File").menu("New").menu("Project...").click();
		bot.tree().getTreeItem("C/C++").doubleClick();

		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").select();
		bot.tree().getTreeItem("C/C++").getNode("C/C++ Project").doubleClick();
		bot.table().select(0);
		bot.table().getTableItem(0).doubleClick();
		bot.textWithLabel("&Project name:").setText("sf1");
		bot.tree().getTreeItem("Executable").getNode("RISCV C Project (SF1)").select();
		bot.button("Next >").click();
		bot.button("Finish").click();
		
		//bot.contextMenu("").click();
		
		
		bot.viewByTitle("Project Explorer").show();
		// 展开 sf1 -> sf1_sdk
		SWTBotTreeItem sf1 = bot.tree().getTreeItem("sf1").expand();
		SWTBotTreeItem sdk = sf1.getNode("sf1_sdk").expand();

		// 打开 New > Other...
		bot.tree().getTreeItem("sf1").getNode("sf1_sdk").contextMenu("New").menu("File").click();
		

		// 填写文件名
		bot.textWithLabel("File na&me:").setText("main.c");
		bot.button("Finish").click();

		// ✅ 等待文件系统/视图刷新
		Thread.sleep(1000); // 保守等待1秒，或者你可以使用 waitUntil 更好

		// ✅ 强制刷新 + 重新展开
		bot.tree().setFocus();
		bot.tree().pressShortcut(SWT.F5, (char) 0);

		sf1 = bot.tree().getTreeItem("sf1").expand(); // 重新获取 TreeItem 防止对象失效
		sdk = sf1.getNode("sf1_sdk").expand();

		// ✅ 打印调试：确认 main.c 出来了没
		System.out.println("文件列表：");
		for (String node : sdk.getNodes()) {
		    System.out.println(" - " + node);
		}

		// ✅ 获取 main.c 节点
		SWTBotTreeItem sourceFile = sdk.getNode("main.c");
		sourceFile.select();
		sourceFile.setFocus();
		sourceFile.pressShortcut(SWT.CTRL, 'c'); // 复制

		// ✅ 粘贴到 application
		SWTBotTreeItem targetFolder = sf1.getNode("application").select();
		targetFolder.setFocus();
		targetFolder.pressShortcut(SWT.CTRL, 'v');
		
		//Thread.sleep(100000);
	}
	
	
	

	

}
